package gw.com.android.ui.me;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import gw.com.android.ui.BaseActivity;
import gw.com.android.ui.dialog.DeleteConfirmDialog;

/**
 * @author jett
 * @since 2017-10-23.
 */
public class PreviewActivity extends BaseActivity {

    public static final String IMAGE_PATH = "image_path";
    public static final String IMAGE_INDEX = "image_index";

    @BindView(R.id.vp_images)
    ViewPager vp;
    private ArrayList<String> datas;
    private ArrayList<String> deleteList = new ArrayList<>();

    @Override
    protected int getLayoutView() {
        return R.layout.activity_preview;
    }

    @Override
    protected void initLayoutView() {
        mTitleBar.setRightIcon(R.mipmap.a_nav_delete);
    }

    @Override
    protected void initViewData() {
        datas = getIntent().getStringArrayListExtra(IMAGE_PATH);
        if (datas == null || datas.isEmpty()) {
            finish();
            return;
        }
        int index = getIntent().getIntExtra(IMAGE_INDEX, 0);
        mTitleBar.setAppTitle(String.format(Locale.getDefault(), "%d/%d", index + 1, datas.size()));
        vp.setAdapter(new MyAdapter());

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mTitleBar.setAppTitle(String.format(Locale.getDefault(), "%d/%d", position + 1, datas.size()));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        vp.setCurrentItem(index);
    }

    @OnClick(R.id.title_right_icon)
    void delete() {
        new DeleteConfirmDialog(this).listener(new DeleteConfirmDialog.OnDeleteListener() {
            @Override
            public void onDelete(DeleteConfirmDialog dialog, View view) {
                int current = vp.getCurrentItem();
                if (datas != null && datas.size() > current) {
                    String deletePath = datas.remove(current);
                    deleteList.add(deletePath);
                    if (datas.isEmpty()) {
                        finish();
                        return;
                    }
                }
                vp.setAdapter(new MyAdapter());
                vp.setCurrentItem(current);
                mTitleBar.setAppTitle(String.format(Locale.getDefault(), "%d/%d", vp.getCurrentItem() + 1, datas.size()));
            }
        }).show();
    }

    @Override
    public void finish() {
        Intent data = new Intent();
        data.putStringArrayListExtra("data", deleteList);
        setResult(RESULT_OK, data);
        super.finish();
    }

    private class MyAdapter extends PagerAdapter {

        private LinkedList<ImageView> recycledViews = new LinkedList<>();

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view = obtainItemView();
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            container.addView(view, params);
            String path = getItem(position);
            if (!TextUtils.isEmpty(path)) {
                Glide.with(PreviewActivity.this).load(path).into(view);
            }
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ImageView iv = (ImageView) object;
            container.removeView(iv);
            recyclerItemView(iv);
        }

        private String getItem(int position) {
            if (datas == null || datas.size() <= position) {
                return null;
            }
            return datas.get(position);
        }

        @Override
        public int getCount() {
            return datas != null ? datas.size() : 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        private ImageView obtainItemView() {
            ImageView view = recycledViews.pollFirst();
            if (view == null) {
                view = new ImageView(PreviewActivity.this);
            }
            return view;
        }

        private void recyclerItemView(ImageView view) {
            view.setImageResource(0);
            recycledViews.addLast(view);
        }

    }

}
